package com.kuanter.kuanterauto.chexian;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kuanter.adapter.TestExpandablelistAdapter1;
import com.kuanter.kuanterauto.R;
import com.kuanter.kuanterauto.animation.Animations;
import com.kuanter.kuanterauto.baseui.BaseActivity_NoBar;
import com.kuanter.kuanterauto.view.CustomExpandableListView1;
import com.kuanter.kuanterauto.view.CustomLetterView.OnTouchingLetterChangedListener;
import com.umeng.analytics.MobclickAgent;

/**
 * 选择保险界面
 * 
 * @author soup
 * 
 */
public class XzbxActivity extends BaseActivity_NoBar implements
		OnChildClickListener, OnGroupCollapseListener,
		OnTouchingLetterChangedListener {

	private TestExpandablelistAdapter1 testExpandablelistAdapter;

	private CustomExpandableListView1 expandable_xzbx;
	public static RelativeLayout rl_xzbx;
	public static ImageView iv_yy_xzbx;
	private Button btTopLeft_xzbx = null;
	private TextView tv_xzbx = null;
	private ListView lv_xzbx = null;

	private Animations a = null;
	int i = 0;
	
	private String city="";
	private String bx="";

	private static final String[] SF = { "直辖市", "湖北", "湖南", "河南", "山东", "江苏",
			"四川", "陕西", "广东" };
	private static final String[] BX={"华泰保险","天安保险","中国平安","中国人保","大地保险","太平洋保险","阳光保险","太平财险","紫金财险","天平汽车保险","中华联合","长安保险"};
	
	private ArrayList<String> groupList = new ArrayList<String>();
	private ArrayList<ArrayList<String>> childList = new ArrayList<ArrayList<String>>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.xzbx);

		initGroupList();
		initChildList();

		initView();
		setListener();
		initUI();
		
	}

	private void initView() {
		a = new Animations(this);
		rl_xzbx = (RelativeLayout) findViewById(R.id.rl_xzbx);
		tv_xzbx = (TextView) findViewById(R.id.tv_xzbx);
		lv_xzbx = (ListView) findViewById(R.id.lv_xzbx);
		btTopLeft_xzbx = (Button) findViewById(R.id.btTopLeft_xzbx);
		iv_yy_xzbx = (ImageView) findViewById(R.id.iv_yy_xzbx);

		expandable_xzbx = (CustomExpandableListView1) findViewById(R.id.expandable_xzbx);
		expandable_xzbx.setHeaderView(getLayoutInflater().inflate(
				R.layout.expandablelist_group_item, expandable_xzbx, false));

		i = rl_xzbx.getVisibility();
	}

	private void initUI() {
		// 设置adapter并且全部展开
		testExpandablelistAdapter = new TestExpandablelistAdapter1(this,
				expandable_xzbx, groupList, childList);
		expandable_xzbx.setAdapter(testExpandablelistAdapter);
		int groupSize = groupList.size();
		for (int i = 0; i < groupSize; i++) {
			expandable_xzbx.expandGroup(i);
		}
		// Log.i("snow", "groupList=" + groupList);
		// Log.i("snow", "childList=" + childList);
		lv_xzbx.setAdapter(new Adapter());
	}

	/**
	 * 设置监听
	 */
	private void setListener() {
		btTopLeft_xzbx.setOnClickListener(this);
		expandable_xzbx.setOnChildClickListener(this);
		expandable_xzbx.setOnGroupCollapseListener(this);
		expandable_xzbx.setOnGroupClickListener(new OnGroupClickListener() {
			@Override
			public boolean onGroupClick(ExpandableListView parent, View v,
					int groupPosition, long id) {
				return false;
			}
		});
		
		lv_xzbx.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				bx=BX[arg2];
				
				Intent intent=new Intent();
				intent.putExtra("city", city);
				intent.putExtra("bx", bx);
				setResult(XgclActivity.XZBX, intent);
				finish();
//				rl_xzbx.setVisibility(View.GONE);
//				iv_yy_xzbx.setVisibility(View.GONE);
//				showToast("您选择了："+city+"-"+bx);
			}
		
		});
	}
	
	@Override
	protected void onResume() {
		MobclickAgent.onResume(this);
		super.onResume();
	}
	
	@Override
	protected void onPause() {
		MobclickAgent.onPause(this);
		super.onPause();
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.btTopLeft_xzbx:
			finish();
			break;
		}
	}

	@Override
	public void onTouchingLetterChanged(int groupPosition) {

	}

	@Override
	public void onGroupCollapse(int groupPosition) {
		if (expandable_xzbx.isGroupExpanded(groupPosition)) {
			return;
		} else {
			expandable_xzbx.expandGroup(groupPosition);
		}
	}

	@Override
	public boolean onChildClick(ExpandableListView parent, View v,
			int groupPosition, int childPosition, long id) {
		city=childList.get(groupPosition).get(childPosition);
		tv_xzbx.setText(city);
		
		i=rl_xzbx.getVisibility();
		
		if (i == View.GONE) {
			rl_xzbx.setAnimation(a.t_Animation());
			rl_xzbx.setVisibility(View.VISIBLE);
			iv_yy_xzbx.setVisibility(View.VISIBLE);
		}
		return false;
	}

	private void initGroupList() {
		for (int i = 0; i < SF.length; i++) {
			groupList.add(SF[i]);
		}
		
	}

	// private static final String[] SF = { "直辖市", "湖北", "湖南", "河南", "山东", "江苏",
	// "四川", "陕西", "广东" };
	private void initChildList() {
		ArrayList<String> A = new ArrayList<String>();
		A.add("北京");
		A.add("上海");
		A.add("重庆");

		ArrayList<String> B = new ArrayList<String>();
		B.add("武汉");

		ArrayList<String> C = new ArrayList<String>();
		C.add("长沙");

		ArrayList<String> D = new ArrayList<String>();
		D.add("郑州");

		ArrayList<String> E = new ArrayList<String>();
		E.add("济南");

		ArrayList<String> F = new ArrayList<String>();
		F.add("南京");

		ArrayList<String> G = new ArrayList<String>();
		G.add("成都");

		ArrayList<String> H = new ArrayList<String>();
		H.add("西安");

		ArrayList<String> I = new ArrayList<String>();
		I.add("广州");
		I.add("深圳");

		childList.add(A);
		childList.add(B);
		childList.add(C);
		childList.add(D);
		childList.add(E);
		childList.add(F);
		childList.add(G);
		childList.add(H);
		childList.add(I);
	}
	
	class Adapter extends BaseAdapter{

		@Override
		public int getCount() {
			return BX.length;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return BX[position];
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			Holder holder=null;
			if(convertView==null){
				holder=new Holder();
				convertView=LayoutInflater.from(XzbxActivity.this).inflate(R.layout.lv_bx_item, null);
				holder.tv_pp=(TextView) convertView.findViewById(R.id.tv_bx_);
				holder.rel_bx_x=(RelativeLayout) convertView.findViewById(R.id.rel_bx_x);
				
				convertView.setTag(holder);
			}else{
				holder=(Holder) convertView.getTag();
			}
			holder.tv_pp.setText(BX[position]);
			
//			if(!convertView.isFocused()){
//				if(position==BX.length-1){
//					holder.rel_bx_x.setVisibility(View.GONE);
//				}
//				}
			
			return convertView;
		}
		
		class Holder{
			TextView tv_pp;
			RelativeLayout rel_bx_x;
		}
		
	}


}
